package com.ep.baselibrary.http;

import android.content.Context;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/3.
 */

public class HttpUtils {

    private String mUrl;

    //请求方式,默认post
    private int mType = POST_TYPE;
    private static final int POST_TYPE = 0x001;
    private static final int GET_TYPE = 0x002;

    //参数
    private Map<String, Object> mParams;

    //网络引擎
    private static IHttpEngine mIHttpEngine = null;

    //上下文
    private Context mContext;

    //是否缓存
    private boolean mCache = false;
    //请求body
    private String body;

    private HttpUtils(Context context) {
        this.mContext = context;
        mParams = new HashMap<>();
    }

    public static HttpUtils with(Context context) {
        return new HttpUtils(context);
    }

    //请求方式
    public HttpUtils post() {
        mType = POST_TYPE;
        return this;
    }

    public HttpUtils get() {
        mType = GET_TYPE;
        return this;
    }

    //设置缓存
    public HttpUtils cache(boolean isCache) {
        mCache = isCache;
        return this;
    }

    //添加参数
    public HttpUtils addParam(String key, Object value) {
        mParams.put(key, value);
        return this;
    }

    public HttpUtils addParams(Map<String, Object> map) {
        mParams.putAll(map);
        return this;
    }

    //body
    public HttpUtils body(String requestBody) {
        body = requestBody;
        return this;
    }

    //添加回调执行
    public void execute(EngineCallBack callback) {
        if (callback == null) {
            callback = EngineCallBack.DEFAULT_CALL_BACK;
        }

        //判断执行方法
        if (mType == POST_TYPE) {
            post(mUrl, body, callback);
        }
        if (mType == GET_TYPE) {
            get(mUrl, mParams, callback);
        }
    }

    public void execute() {
        execute(null);
    }

    //初始化网络引擎
    public static void init(IHttpEngine iHttpEngine) {
        mIHttpEngine = iHttpEngine;
    }

    //自带网络引擎
    public HttpUtils exchangeEngine(IHttpEngine iHttpEngine) {
        mIHttpEngine = iHttpEngine;
        return this;
    }

    public void get(String url, Map<String, Object> params, EngineCallBack callback) {
        mIHttpEngine.get(mCache, mContext, url,params, callback);
    }

    public void post(String url, String body, EngineCallBack callback) {
        mIHttpEngine.post(mCache, mContext, url, body, callback);
    }

    /**
     * 拼接参数
     */
    public static   String jointParams(String url, Map<String, Object> params) {
        if (params == null || params.size() <= 0) {
            return url;
        }

        StringBuffer stringBuffer = new StringBuffer(url);
        if (!url.contains("?")) {
            stringBuffer.append("?");
        } else {
            if (!url.endsWith("?")) {
                stringBuffer.append("&");
            }
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {
            stringBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
        }

        stringBuffer.deleteCharAt(stringBuffer.length() - 1);

        return stringBuffer.toString();
    }

    /**
     * 解析一个类上面的class信息
     */
    public static Class<?> analysisClazzInfo(Object object) {
        Type genType = object.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        return (Class<?>) params[0];
    }
}
